package com.in.mobilerechargedth;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.in.mobilerechargedth.adapter.SpinnerAdapter;
import com.in.mobilerechargedth.api.RechargeApi;
import com.in.mobilerechargedth.constant.ConstantVariables;
import com.in.mobilerechargedth.model.RechargeModel;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class FourthScreenActivity extends BaseActivity {

    Button btnGo;
    Spinner spnCircle, spnAmount;

    private List<String> listCircle, listAmount;

    public static List<RechargeModel> listRechargeModel;
    Button backbtn;

    int operatorId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fourth_screen);
        String strOperatorId = getIntent().getStringExtra(ConstantVariables.OPERATOR_ID);
        operatorId = Integer.parseInt(strOperatorId);
        listCircle = new ArrayList<String>();
        listAmount = new ArrayList<String>();
        backbtn = (Button) findViewById(R.id.backbtn);
        backbtn.setVisibility(View.VISIBLE);
        backbtn.setOnClickListener(this);
        btnGo = (Button) findViewById(R.id.btnGo);
        spnCircle = (Spinner) findViewById(R.id.spnCircle);
        spnAmount = (Spinner) findViewById(R.id.spnAmount);

        //
        btnGo.setOnClickListener(this);

        new RechargeTask().execute();
        setupAdmob();
        share();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        super.onClick(v);
        switch (v.getId()) {
        case R.id.btnGo:
            if (spnCircle.getSelectedItem() != null && spnAmount.getSelectedItem() != null) {
                Intent i = new Intent(FourthScreenActivity.this,
                        FourthRechargeDetailScreenActivity.class);
                i.putExtra(ConstantVariables.EXTRA_CIRCLE, spnCircle
                        .getSelectedItem().toString());
                i.putExtra(ConstantVariables.EXTRA_AMOUNT, spnAmount
                        .getSelectedItem().toString());
                i.putExtra(ConstantVariables.OPERATOR_ID, operatorId + "");
                Log.e("Operator_id second put", operatorId + "");
                startActivity(i);
            }
            break;
        case R.id.backbtn:
            this.onBackPressed();
            break;
        }
    }

    /**
     * Async Task to proceed Operator.
     */
    private class RechargeTask extends
            AsyncTask<Void, Void, List<RechargeModel>> {

        private ProgressDialog mProgress;
        String circle;

        @Override
        protected void onPreExecute() {
            mProgress = new ProgressDialog(FourthScreenActivity.this);
            mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgress.setMessage("Loading...");
            mProgress.setCanceledOnTouchOutside(false);
            // Check press back key when showing dialog.
            mProgress.setOnKeyListener(new DialogInterface.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode,
                        KeyEvent event) {
                    // TODO Auto-generated method stub
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
            mProgress.show();

            super.onPreExecute();
        }

        @Override
        protected List<RechargeModel> doInBackground(Void... params) {
            RechargeApi api = new RechargeApi();
            List<RechargeModel> data = null;
            try {
                data = api.getRecharge(operatorId);
            } catch (Exception e) {
                Log.e("getCircle", e.getMessage());
            }

            return data;
        }

        @Override
        protected void onPostExecute(List<RechargeModel> result) {
            super.onPostExecute(result);

            if (result != null) {
                listRechargeModel = result;
                for (RechargeModel rechargeModel : result) {
                    int flag = 0;
                    for (int i = 0; i < listCircle.size(); i++) {
                        if (listCircle.get(i).equals(
                                rechargeModel.getCircleName())) {
                            flag = 1;
                        }
                    }
                    Log.e("getCircle", rechargeModel.getCircleName());
                    if (flag == 0) {
                        listCircle.add(rechargeModel.getCircleName());
                    }

                }
                Collections.sort(listCircle);
                SpinnerAdapter circleAdapter = new SpinnerAdapter(
                        FourthScreenActivity.this,
                        android.R.layout.simple_spinner_item, listCircle);
                circleAdapter
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spnCircle.setAdapter(circleAdapter);
                spnCircle
                        .setOnItemSelectedListener(new OnItemSelectedListener() {

                            @Override
                            public void onItemSelected(AdapterView<?> arg0,
                                    View arg1, int arg2, long arg3) {
                                // TODO Auto-generated method stub
                                circle = spnCircle.getSelectedItem().toString();
                                Log.e("aaaaaaaaa", circle);

                                listAmount = new ArrayList<String>();
                                for (RechargeModel rechargeModel : listRechargeModel) {
                                    int flag_recharge = 0;
                                    for (int j = 0; j < listAmount.size(); j++) {
                                        if (listAmount.get(j).equals(
                                                rechargeModel.getAmount())) {
                                            flag_recharge = 1;
                                        }
                                    }

                                    if (flag_recharge == 0
                                            && circle.equals(rechargeModel
                                                    .getCircleName())
                                            && rechargeModel.getOperatorId() == operatorId) {
                                        listAmount.add(rechargeModel.getAmount());
                                        Log.e("getRecharge get",
                                                rechargeModel.getAmount());
                                    }

                                }
                                Collections.sort(listAmount);
                                // recharge plan
                                SpinnerAdapter amountAdapter = new SpinnerAdapter(
                                        FourthScreenActivity.this,
                                        android.R.layout.simple_spinner_item,
                                        listAmount);
                                amountAdapter
                                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                                spnAmount.setAdapter(amountAdapter);
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> arg0) {
                                // TODO Auto-generated method stub

                            }
                        });
            }
            if (mProgress != null) {
                mProgress.dismiss();
                mProgress = null;
            }

        }

    }
}
